package com.fishingwithme.android.utils

import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import java.util.UUID

object EventBus {
    private val _unauthorizedEvent = MutableSharedFlow<Unit>(replay = 1)
    val unauthorizedEvent = _unauthorizedEvent.asSharedFlow()
    
    fun postUnauthorizedEvent() {
        android.util.Log.d("EventBus", "发布未授权事件")
        _unauthorizedEvent.tryEmit(Unit)
    }

    // Map data event handling
    private val _mapDataEvent = MutableSharedFlow<MapDataEvent>(replay = 1)
    val mapDataEvent = _mapDataEvent.asSharedFlow()
    
    fun postMapDataEvent(event: MapDataEvent) {
        android.util.Log.d("EventBus", "发布地图数据事件，数据大小: ${event.data.size}")
        val result = _mapDataEvent.tryEmit(event)
        android.util.Log.d("EventBus", "地图数据事件发布结果: $result")
    }
    
    // Navigation event handling
    private val _navigationEvent = MutableSharedFlow<NavigationEvent>(replay = 1)
    val navigationEvent = _navigationEvent.asSharedFlow()
    
    fun postNavigationEvent(destination: String, sessionId: String? = null) {
        val event = NavigationEvent(destination, sessionId)
        android.util.Log.d("EventBus", "发布导航事件，目标: $destination, 会话ID: ${event.sessionId}")
        val result = _navigationEvent.tryEmit(event)
        android.util.Log.d("EventBus", "导航事件发布结果: $result")
    }
    
    /**
     * Navigation event class with session ID for proper event targeting
     */
    data class NavigationEvent(val destination: String, val sessionId: String? = null)
}